//
//  CategoryViewController.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "CategoryViewController.h"

#define URLSTR  @"http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=android&picVersion=11&scale=2"

@interface CategoryViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, CategoryDelegate>

@property (nonatomic, strong)UICollectionView *categoryV;
@property (nonatomic, strong)NSMutableArray *categoryArr;
@property (nonatomic, strong)NSMutableArray *categoryListArr;
@property (nonatomic, strong)NSMutableArray *categoryImageArr;

@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);
    flowLayout.minimumInteritemSpacing = 0.5;
    flowLayout.minimumLineSpacing = 1;
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 2 - 0.5, SCREEN_HEIGHT / 13);
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT / 3.55);
    self.categoryV = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    _categoryV.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    _categoryV.delegate = self;
    _categoryV.dataSource = self;
    [self.view addSubview:_categoryV];
    
    [_categoryV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-50);
        
    }];
    
    
    [_categoryV registerClass:[LBCCategoryCell class] forCellWithReuseIdentifier:@"categoryCell"];
    [_categoryV registerClass:[LBCCategoryHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LBCCategoryHeadView"];
    
    self.categoryArr = [NSMutableArray array];
    self.categoryImageArr = [NSMutableArray array];
    self.categoryListArr = [NSMutableArray array];
    [self getData];
}

- (void)getData{
    
    [AFNetworkTool getUrl:URLSTR body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
             LBCCategory *category = [LBCCategory new];
            [category setValuesForKeysWithDictionary:dic];
            [_categoryArr addObject:category];
        }
        for (int i = 0; i < 5; i++) {
            _categoryImageArr[i] = _categoryArr[i];
        }
        for (int i = 5; i < _categoryArr.count; i++) {
            _categoryListArr[i - 5] = _categoryArr[i];
        }
        [_categoryV reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (3 == section) {
        return 2;
    }
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *idntifier = @"categoryCell";
    LBCCategoryCell *categoryCell = [collectionView dequeueReusableCellWithReuseIdentifier:idntifier forIndexPath:indexPath];
    categoryCell.backgroundColor = [UIColor whiteColor];
    NSMutableArray *array = [NSMutableArray array];
    
    if (_categoryListArr.count != 0) {
        if (0 == indexPath.section) {
            for (int i = 0; i < 6; i++) {
                array[i] = _categoryListArr[i];
            }
        }
        if (1 == indexPath.section) {
            for (int i = 6; i < 12; i++) {
                array[i - 6] = _categoryListArr[i];
            }
        }
        if (2 == indexPath.section) {
            for (int i = 12; i < 18; i++) {
                array[i - 12] = _categoryListArr[i];
            }
        }
        if (3 == indexPath.section) {
            for (int i = 18; i < _categoryListArr.count; i++) {
                array[i - 18] = _categoryListArr[i];
            }
        }
        categoryCell.category = array[indexPath.item];
    } else {
        
    }
    return categoryCell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        LBCCategoryHeadView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"LBCCategoryHeadView" forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
        reusableView.categorydelegate = self;
        if (_categoryImageArr.count != 0) {
            reusableView.categroyArr = _categoryImageArr;
        } else {
            
        }
        return reusableView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (0 == section) {
        return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT / 3.55);
    }
    return CGSizeMake(0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LBCCategoryListViewController *categoryListVC = [LBCCategoryListViewController new];
    NSMutableArray *array = [NSMutableArray array];
    if (0 == indexPath.section) {
        for (int i = 0; i < 6; i++) {
            array[i] = _categoryListArr[i];
        }
    }
    if (1 == indexPath.section) {
        for (int i = 6; i < 12; i++) {
            array[i - 6] = _categoryListArr[i];
        }
    }
    if (2 == indexPath.section) {
        for (int i = 12; i < 18; i++) {
            array[i - 12] = _categoryListArr[i];
        }
    }
    if (3 == indexPath.section) {
        for (int i = 18; i < _categoryListArr.count; i++) {
            array[i - 18] = _categoryListArr[i];
        }
    }
    LBCCategory *cate = array[indexPath.item];
    categoryListVC.category = cate;
    categoryListVC.categoryId = cate.categoryId;
    categoryListVC.contentType = cate.contentType;
    [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:categoryListVC];
    [self.navigationController pushViewController:categoryListVC animated:YES];
    
}

- (void)presentToListViewControllerTag:(NSInteger)tag{
    LBCCategoryListViewController *categoryListVC = [LBCCategoryListViewController new];
    NSInteger index = tag % 10;
    LBCCategory *cate = _categoryImageArr[index];
    categoryListVC.category = cate;
    categoryListVC.categoryId = cate.categoryId;
    categoryListVC.contentType = cate.contentType;
    [(KBTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:categoryListVC];
    [self.navigationController pushViewController:categoryListVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
